﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3DFileConverter
{
    class StlWriter {

        public void writeHeader(uint numberOfFacets) {
            //string header = "This is header\n";
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\figureBin.stl";
            using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.OpenOrCreate))){
                Byte header = 0;
                for (int i = 0; i < 80; i++) {
                    writer.Write(header);
                }
                writer.Write(numberOfFacets);
            }
        }

        public void stlFormatWrite(StlFigure stlFigure) {

            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\figureBin.stl";
            UInt16 attributeByte = 0;

            using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Append))){
                
                writer.Write((float)0);
                writer.Write((float)0);
                writer.Write((float)0);
                
                writer.Write(stlFigure.Verteces.ElementAt(0).X);
                writer.Write(stlFigure.Verteces.ElementAt(0).Y);
                writer.Write(stlFigure.Verteces.ElementAt(0).Z);

                writer.Write(stlFigure.Verteces.ElementAt(1).X);
                writer.Write(stlFigure.Verteces.ElementAt(1).Y);
                writer.Write(stlFigure.Verteces.ElementAt(1).Z);

                writer.Write(stlFigure.Verteces.ElementAt(2).X);
                writer.Write(stlFigure.Verteces.ElementAt(2).Y);
                writer.Write(stlFigure.Verteces.ElementAt(2).Z);

                writer.Write(attributeByte);
            }
        }
    }
}
